const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class PhotosHandler extends BaseHandler {

    get pattern() {
        return '/wall/:wall_id/photos';
    }

    //TODO: Need refactor
    doPOST(request, response) {
        var me = this;
        var message = request.body.message;
        var serverId = request.body.serverId;
        if(serverId) {
            var ratio = request.body.ratio;
            var link = request.body.additional_url;
            var userInfo = request.session.walls[request.params.wall_id];
            userInfo.host = request.headers.host;
            this.Controller.saveOrUpdateWallPhoto(request.params.wall_id, message, serverId, ratio, link, userInfo, (result) => {
                response.json(result);
            });
        } else {
            var user_info = request.session.user;
            var data = request.body;

            this.Controller.savePhotoAssset(request.params.wall_id, request.body, user_info, (result) => {
                response.json(result);
            })
        }
    }

    doGET(request, response) {
        var isBusinessUser = false;
        var count = request.query.count || 5;
        var type = request.query.type || 'new'; //new or old
        var since = request.query.since || 'now';
        var platform = request.query.platform || '';
        var mode = request.query.mode || 'latest'; // latest or hot
        var source = request.query.source || null;
        var wallId = request.params.wall_id;
        var userId = request.session.user.id || (request.session.walls[request.params.wall_id] && request.session.walls[request.params.wall_id].wall_user_id) || -1;
        var status = isBusinessUser ? (request.query.status ? request.query.status : null) : (request.query.status || 'approved');

        this.Controller.getPhotosByWallId(wallId, {
            count: count, 
            type: type,
            since: since,
            status: status,
            mode: mode,
            platform: platform,
            source: source,
            userId: userId
        }, (result) => {
            response.json({
                success: true,
                data: result
            });
        });
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = PhotosHandler;